//
//  SetInviteCodeVC.swift
//  LeTu
//
//  Created by gy on 2023/12/20.
//

import UIKit

class SetInviteCodeVC: BaseViewController {

    private var viewModel = SetInviteCodeVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        DispatchQueue.main.async {
            self.contentBackView.layerCornerRadius(24, [.topLeft, .topRight])
            self.topImgView.addGradientLayer(gradientColors: [UIColor.app_themeStartColor, UIColor.app_themeEndColor], gradientDirection: .vertically, startPoint: CGPoint(x: 0.25, y: 0.5), endPoint: CGPoint(x: 0.75, y: 0.5))
            self.commitBtn.layerCornerRadius(CGFloat.app_btnHeight/2)
            self.promptBackView.layerCornerRadius(10)
        }
        
    }
    
    // MARK: - UIButtonAction
    @objc func commitBtnAction() {
    
        
//        InviteCodeAlertView {
//            guard let url = URL(string: "weixin://") else { return }
//            if UIApplication.shared.canOpenURL(url) {
//                UIApplication.shared.open(url)
//            }
//        }.show()
        
        guard let invite = inputInviteView.contentTextField.text, invite.count > 0 else {
            if let placeholder = inputInviteView.contentTextField.placeholder {
                showMessage(message: placeholder)
            }
            return
        }
        
        viewModel.setInvite(params: ["invitationCode":invite]) { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                strongSelf.showMessage(message: dialog.message.localized)
                if dialog.code == 200 {
                    kUserManager.userModel.parentInvitationCode = invite
                    strongSelf.backAction()
                }
            }
            
        }
        
    }
    private func updateCommitBtnUI() {
        let invitationCode = kUserManager.userModel.parentInvitationCode
        if invitationCode.count == 0 {
            commitBtn.gradientColors = UIColor.app_themeGradientColors
        } else {
            commitBtn.gradientColors = [UIColor.app_btnDisableBackColor.cgColor, UIColor.app_btnDisableBackColor.cgColor]
        }
        commitBtn.isEnabled = invitationCode.count == 0
        
        inputInviteView.contentTextField.isEnabled = invitationCode.count == 0
        inputInviteView.contentTextField.text = invitationCode
    }
    
    // MARK: - configUI
    func configUI() {
        
        view.addSubview(topImgView)
        view.addSubview(backBtn)
        view.addSubview(topPromptLabel)
        view.addSubview(contentBackView)
        contentBackView.addSubview(inputInviteView)
        contentBackView.addSubview(commitBtn)
        contentBackView.addSubview(promptBackView)
        promptBackView.addSubview(promptTitleLabel)
        promptBackView.addSubview(promptLabel)
        
        topImgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(263)
        }
        backBtn.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.width.equalTo(62)
            make.height.equalTo(Size.navigationBarWithoutStatusHeight)
            make.top.equalTo(Size.statusBarHeight)
        }
        topPromptLabel.snp.makeConstraints { make in
            make.top.equalTo(Size.navigationBarHeight)
            make.left.equalTo(24)
            make.right.equalTo(-24)
        }
        contentBackView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(topPromptLabel.snp.bottom).offset(62)
        }
        inputInviteView.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(50)
            make.top.equalTo(22)
        }
        commitBtn.snp.makeConstraints { make in
            make.left.right.equalTo(inputInviteView)
            make.top.equalTo(inputInviteView.snp.bottom).offset(20)
            make.height.equalTo(CGFloat.app_btnHeight)
        }
        promptBackView.snp.makeConstraints { make in
            make.left.right.equalTo(inputInviteView)
            make.top.equalTo(commitBtn.snp.bottom).offset(32)
        }
        promptTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(32)
        }
        promptLabel.snp.makeConstraints { make in
            make.left.right.equalTo(promptTitleLabel)
            make.top.equalTo(promptTitleLabel.snp.bottom).offset(8)
            make.bottom.equalTo(-16)
        }
        
        backBtn.addTarget(self, action: #selector(backAction))
        commitBtn.addTarget(self, action: #selector(commitBtnAction))
        inputInviteView.contentTextField.delegate = self
        
        updateCommitBtnUI()
    }
    private lazy var topImgView = UIImageView()
    private lazy var backBtn: CustomClickableView = {
        let backBtn = CustomClickableView()
        backBtn.setImage(UIImage(named: "back_white"))
        let top = (Size.navigationBarWithoutStatusHeight-22)/2
        backBtn.setImageEdgeInsets(UIEdgeInsets(top: top, left: 20, bottom: top, right: 20))
        return backBtn
    }()
    private lazy var topPromptLabel: UILabel = {
        let topPromptLabel = UILabel()
        topPromptLabel.font = kSetPingFangBlod(24)
        topPromptLabel.textColor = .app_white
        topPromptLabel.attributed.text = "填写邀请码立得".localized+"\("3元现金".localized, .foreground(.app_yellow))"
        topPromptLabel.numberOfLines = 0
        topPromptLabel.textAlignment = .center
        return topPromptLabel
    }()
    private lazy var contentBackView: UIView = {
        let contentBackView = UIView()
        contentBackView.backgroundColor = .app_white
        return contentBackView
    }()
    private lazy var inputInviteView = LoginInputView(placeholder: "输入好友的邀请码，可得3元现金".localized)
    private lazy var commitBtn: GradientButton = {
        let commitBtn = GradientButton()
        commitBtn.setTitle("提交".localized)
        return commitBtn
    }()
    private lazy var promptBackView: UIView = {
        let promptBackView = UIView()
        promptBackView.backgroundColor = UIColor(hex: 0xFFFAEE)
        return promptBackView
    }()
    private lazy var promptTitleLabel: UILabel = {
        let promptTitleLabel = UILabel()
        promptTitleLabel.text = "温馨提示".localized
        promptTitleLabel.textColor = .app_themeEndColor
        promptTitleLabel.font = kSetPingFangRegular(12)
        return promptTitleLabel
    }()
    private lazy var promptLabel: UILabel = {
        let promptLabel = UILabel()
        promptLabel.attributed.text = "\("\("1、用户下载本APP后，绑定好友的邀请码，即刻获得现金奖励；".localized)\n\("2、一个账号只能绑定一次好友的邀请码，绑定后将无法再次绑定其他邀请码；".localized)\n\("3、非正常用户不能绑定邀请码，且会被系统记录。".localized)".localized, .paragraph(.lineSpacing(2)))"
        promptLabel.textColor = .app_themeEndColor
        promptLabel.font = kSetPingFangRegular(12)
        promptLabel.numberOfLines = 0
        return promptLabel
    }()

}

extension SetInviteCodeVC: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
}

